<?php

namespace catchAdmin\basics\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\basics\model\Country as CountryModel;
use catcher\CatchAuth;

class Country extends CatchController
{
    
    protected $countryModel;
    
    /**
     *
     * @time 2022/03/04 16:06
     * @param CountryModel $countryModel
     * @return mixed
     */
    public function __construct(CountryModel $countryModel)
    {
        $this->countryModel = $countryModel;
    }
    
    /**
     *
     * @time 2022/03/04 16:06
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->countryModel->getList($user['tenant_id']));
    }

    /**
     * 新建
     * @time 2022/03/02 10:39
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function create(CatchAuth $auth,Request $request)
    {
        $user = $auth->user();
        $params = $request->filterEmptyField()->post();
        $params['tenant_id'] = $user['tenant_id'];
        return CatchResponse::success($this->countryModel->storeBy($params),'新建成功');
    }
    
    /**
     *
     * @time 2022/03/04 16:06
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function update(Request $request)
    {
        $params = $request->filterEmptyField()->post();
        return CatchResponse::success($this->countryModel->updateBy($params['id'],$params),'更新成功');
    }
    
    /**
     *
     * @time 2022/03/04 16:06
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function delete(Request $request)
    {
        $id = $request->post('id');
        return CatchResponse::success($this->countryModel->deleteBy($id),'删除成功');
    }
}